package lgkj.meicqt20.utils;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.util.ArrayList;
import java.util.List;


/**
 * com.lgkj.meicqt.lgkj.com.com.lgkj.meicqt.util Created by hjx on 2017/5/5 11:25
 *
 * @Company:
 * @Email:1263276841@qq.com
 * @Version $Rev$
 * @UpdateAuthor $Author$ on 2017/5/5$
 */

public class VideoDownLoadUtil {
    private static String TAG = "VideoDownLoadUtil";
    public static String mRootPath = Environment.getExternalStorageDirectory() + File.separator + "lianguang" + File.separator;

    /**
     * 读取本地视频
     *
     * @return
     */
    public static List<String> filePaths() {
        List<String> filePath = new ArrayList<>();
        File file = new File(VideoDownLoadUtil.mRootPath);
        File[] subFile = file.listFiles();
        if (subFile == null || subFile.length == 0) {
            return filePath;
        }
        for (int iFileLength = 0; iFileLength < subFile.length; iFileLength++) {
            // 判断是否为文件夹
            if (!subFile[iFileLength].isDirectory()) {
                String filename = subFile[iFileLength].getName();
                // 判断是否为MP4结尾
                if (filename.trim().toLowerCase().endsWith(".mp4")) {
                    filePath.add(filename);
                }
            }
        }
        return filePath;
    }

    public static String getVideoFullName(String path) {
        return path.substring(path.lastIndexOf("/") + 1);
    }

    /**
     * 删除文件
     */
    public static void deleteFile(String path) {
        File f = new File(mRootPath + path);
        Log.d(TAG, "删除文件:" + (mRootPath + path));
        if (f.exists() && f.isFile()) {
            f.delete();
        }
    }

    /**
     * @param list1 本地视频
     * @param list2 网上获取的视频列表
     */
    public static void getUncontain(List<String> list1, List<String> list2) {
        if (list1 == null) {
            return;
        }
        if (list2 == null) {
            return;
        }
        Log.d(TAG, "list1: " + list1.toString());
        Log.d(TAG, "list2: " + list2.toString());
        if (list2 != null && list2.size() > 0) {
            for (String str1 : list1) {
                if (!list2.contains(str1)) {
                    // 打印出list2没有b,d
                    deleteFile(str1);
                    Log.d(TAG, "getUncontain: " + str1);
                }
            }
        }
    }


    public static void openFile(Context context, String fileName) {
        Log.d(TAG, fileName);
        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setAction(android.content.Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(new File(fileName)), "application/vnd.android.package-archive");
        context.startActivity(intent);
    }

    /**
     * 判断是否下载
     *
     * @param name
     * @return
     */
    static int i = 0;

    public static boolean isDown(String name, String size) {
        File file = new File(mRootPath + name);
        Log.d(TAG, "检查第" + i + "个视频，" + mRootPath + name + "======" + "exists:" + file.exists() + ",fileLength:" + size);
        i++;
        if (file.exists() && file.length() == Long.parseLong(size)) {
            Log.d(TAG, "fileLength:" + file.length());
            return false;
        } else {
            return true;
        }
    }
}
